home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / blankery / blanker / source / blankers / aswarm / blank.c < prev    next >
C/C++ Source or Header  |  1993-08-15  |  9KB  |  317 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3.  
  4. #include <intuition/screens.h>
  5.  
  6. #include <dos/dos.h>
  7. #include <graphics/gfxbase.h>
  8. #include <graphics/gfxmacros.h>
  9. #include <graphics/copper.h>
  10. #include <graphics/videocontrol.h>
  11. #include <hardware/custom.h>
  12. #include <hardware/dmabits.h>
  13. #include <graphics/gfxmacros.h>
  14.  
  15. #include <clib/exec_protos.h>
  16. #include <clib/intuition_protos.h>
  17. #include <clib/graphics_protos.h>
  18. #include <clib/alib_protos.h>
  19. #include <clib/gadtools_protos.h>
  20. #include <clib/utility_protos.h>
  21. #include <clib/alib_protos.h>
  22.  
  23. #include <stdlib.h>
  24.  
  25. #include "ASwarm.h"
  26. #include "/defs.h"
  27. #include "/utility.h"
  28.  
  29. #define MAX_SPEED     6L
  30. #define MAX_WASPS     10L
  31. #define MAX_BEES      500L
  32. #define MAX_TIGHTNESS 10L
  33.  
  34. #define BEEACC  3
  35. #define BEEVEL  17
  36. #define WASPACC 5
  37. #define WASPVEL 21
  38. #define BORDER  5
  39.  
  40. #define BEE_PEN  1
  41. #define WASP_PEN 2
  42.  
  43. #define BEE_COL_NUM 33
  44.  
  45. #define RAND( m ) ( RangeRand( m ) - ( m ) / 2 )
  46.  
  47. #define BXVel( I )    ( SP->ss_X[3][I] )
  48. #define BYVel( I )    ( SP->ss_Y[3][I] )
  49. #define BeeX( P, I )    ( SP->ss_X[P][I] )
  50. #define BeeY( P, I )    ( SP->ss_Y[P][I] )
  51. #define MyWasp( I )    ( SP->ss_MW[I] )
  52. #define WaXVel( I )    ( SP->ss_WX[3][I] )
  53. #define WaYVel( I )    ( SP->ss_WY[3][I] )
  54. #define WaspX( P, I )    ( SP->ss_WX[P][I] )
  55. #define WaspY( P, I )    ( SP->ss_WY[P][I] )
  56.  
  57. struct mPrefObject {
  58.     LONG    Wasps, Bees, Tightness, Speed, Colorcycling, Aimmode;
  59. };
  60.  
  61. struct SwarmStruct {
  62.     WORD ss_Width;
  63.     WORD ss_Height;
  64.     WORD ss_NumWasps;
  65.     WORD *ss_WX[4];
  66.     WORD *ss_WY[4];
  67.     WORD *ss_NB;
  68.     WORD ss_NumBees;
  69.     WORD ss_BeeAcc;
  70.     WORD *ss_X[4];
  71.     WORD *ss_Y[4];
  72.     WORD *ss_MW;
  73. };
  74.  
  75. extern    struct    mPrefObject    nP;
  76. extern        ULONG        Depth, Mode;
  77. extern        UBYTE        *prefData;
  78.  
  79. UWORD BeeColors[BEE_COL_NUM] = {
  80.     0x000F, 0x000F, 0x004F, 0x008F, 0x00BF, 0x00FF, 0x00FB, 0x00F7, 0x00F3, 0x00F0, 0x04F0, 0x08F0, 0x09F0,
  81.     0x0AF0, 0x0BF0, 0x0CF0, 0x0DF0, 0x0EF0, 0x0FF0, 0x0FE0, 0x0FD0, 0x0FC0, 0x0FB0, 0x0F90, 0x0F80, 0x0F70,
  82.     0x0F60, 0x0F50, 0x0F40, 0x0F30, 0x0F20, 0x0F10, 0x0F00
  83. };
  84.  
  85. UWORD SwarmColors[4] = {
  86.     0x0000, 0x0000, 0x0FFF, 0x0000
  87. };
  88.  
  89. BYTE sqrt_tab[256] = {
  90.     0,1,1,1,2,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,
  91.     7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,
  92.     10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
  93.     11,11,11,11,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,13,13,13,13,13,
  94.     13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,14,14,14,14,14,
  95.     14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
  96.     15,15,15,15,15,15,15,15,15,15,15,15
  97. };
  98.  
  99. LONG FastSQRT( LONG x )
  100. {
  101.     LONG sr = 1L;
  102.  
  103.     while( x > 255L ) {
  104.         x /= 4L;
  105.         sr *= 2L;
  106.     }
  107.  
  108.     return( sr * (LONG)sqrt_tab[x] );
  109. }
  110.  
  111. ULONG SwarmSize( LONG NumWaps, LONG NumBees )
  112. {
  113.     return sizeof( struct SwarmStruct ) + sizeof( WORD ) * ( (ULONG)NumWaps * 9L + (ULONG)NumBees * 9L );
  114. }
  115.  
  116. struct SwarmStruct *CreateSwarms( struct Screen *Scr, LONG NumWasps, LONG NumBees, LONG Speed, LONG Tightness )
  117. {
  118.     LONG             Index;
  119.     struct SwarmStruct    *SP;
  120.     WORD            *Ptr;
  121.  
  122.     if(( SP = AllocVec( SwarmSize( NumWasps, NumBees ), 0L )) == NULL) return NULL;
  123.  
  124.     SP->ss_NumWasps = NumWasps;
  125.     SP->ss_NumBees = NumBees;
  126.     SP->ss_Width = Scr->Width;
  127.     SP->ss_Height = Scr->Height;
  128.     SP->ss_BeeAcc = ( Tightness * BEEACC )/MAX_SPEED+1;
  129.  
  130.     Ptr = (WORD *)( &SP[1] );
  131.     for( Index = 0L; Index < 4L; Index++ ) {
  132.         SP->ss_WX[Index] = Ptr;
  133.         Ptr += NumWasps;
  134.         SP->ss_WY[Index] = Ptr;
  135.         Ptr += NumWasps;
  136.         SP->ss_X[Index] = Ptr;
  137.         Ptr += NumBees;
  138.         SP->ss_Y[Index] = Ptr;
  139.         Ptr += NumBees;
  140.     }
  141.     SP->ss_NB = Ptr;
  142.     SP->ss_MW = Ptr + NumWasps;
  143.  
  144.     for( Index = 0L; Index < NumWasps; Index++ ) {
  145.         WaspX( 1, Index ) = WaspX( 0, Index ) = BORDER + RangeRand( SP->ss_Width - 2 * BORDER );
  146.         WaspY( 1, Index ) = WaspY( 0, Index ) = BORDER + RangeRand( SP->ss_Height - 2 * BORDER );
  147.         WaXVel( Index ) = RAND( WASPACC );
  148.         WaYVel( Index ) = RAND( WASPACC );
  149.         SP->ss_NB[Index]=0;
  150.     }
  151.  
  152.     for( Index = 0L; Index < SP->ss_NumBees; Index++ ) {
  153.         BeeX( 1, Index ) = BeeX( 0, Index ) = BORDER + RangeRand( SP->ss_Width - 2 * BORDER );
  154.         BeeY( 1, Index ) = BeeY( 0, Index ) = BORDER + RangeRand( SP->ss_Height - 2 * BORDER );
  155.         BXVel( Index ) = RAND( SP->ss_BeeAcc );
  156.         BYVel( Index ) = RAND( SP->ss_BeeAcc );
  157.         SP->ss_NB[MyWasp( Index ) = Index % SP->ss_NumWasps]++;
  158.     }
  159.     return SP;
  160. }
  161.  
  162. VOID DrawSwarms( struct RastPort *RP, struct SwarmStruct *SP, LONG AimMode )
  163. {
  164.     LONG Index;
  165.  
  166.     for( Index = 0L; Index < SP->ss_NumWasps; Index++ ) {
  167.         WaspX( 2, Index ) = WaspX( 1, Index );
  168.         WaspX( 1, Index ) = WaspX( 0, Index );
  169.         WaspY( 2, Index ) = WaspY( 1, Index );
  170.         WaspY( 1, Index ) = WaspY( 0, Index );
  171.  
  172.         WaXVel( Index ) += RAND( WASPACC );
  173.         WaYVel( Index ) += RAND( WASPACC );
  174.  
  175.         if( WaXVel( Index ) > WASPVEL ) WaXVel( Index ) = WASPVEL;
  176.         if( WaXVel( Index ) < -WASPVEL ) WaXVel( Index ) = -WASPVEL;
  177.         if( WaYVel( Index ) > WASPVEL ) WaYVel( Index ) = WASPVEL;
  178.         if( WaYVel( Index ) < -WASPVEL ) WaYVel( Index ) = -WASPVEL;
  179.  
  180.         WaspX( 0, Index ) = WaspX( 1, Index ) + WaXVel( Index );
  181.         WaspY( 0, Index ) = WaspY( 1, Index ) + WaYVel( Index );
  182.  
  183.         if(( WaspX( 0, Index ) < BORDER )||( WaspX( 0, Index ) > SP->ss_Width-BORDER-1 )) {
  184.             WaXVel( Index ) = -WaXVel( Index );
  185.             if( WaspX( 0, Index ) < BORDER ) WaspX( 0, Index ) = BORDER;
  186.             else WaspX( 0, Index ) = SP->ss_Width-BORDER-1;
  187.         }
  188.  
  189.         if(( WaspY( 0, Index ) < BORDER )||( WaspY( 0, Index ) > SP->ss_Height-BORDER-1 )) {
  190.             WaYVel( Index ) = -WaYVel( Index );
  191.             if( WaspY( 0, Index ) < BORDER ) WaspY( 0, Index ) = BORDER;
  192.             else WaspY(0, Index ) = SP->ss_Height-BORDER-1;
  193.         }
  194.     }
  195.  
  196.     for( Index = 0L; Index < SP->ss_NumBees; Index++ ) {
  197.         WORD DX, DY, ChkIndex;
  198.         LONG Distance, NewDistance;
  199.  
  200.         BeeX( 2, Index ) = BeeX( 1, Index );
  201.         BeeX( 1, Index ) = BeeX( 0, Index );
  202.         BeeY( 2, Index ) = BeeY( 1, Index );
  203.         BeeY( 1, Index ) = BeeY( 0, Index );
  204.  
  205.         DX = WaspX( 1, MyWasp( Index )) - BeeX( 1, Index );
  206.         DY = WaspY( 1, MyWasp( Index )) - BeeY( 1, Index );
  207.         Distance = FastSQRT( DX * DX + DY * DY );
  208.         if( !Distance ) Distance = 1L;
  209.  
  210.         if( AimMode ) {
  211.             for( ChkIndex = 0; ChkIndex <= SP->ss_NumWasps; ChkIndex++ ) {
  212.                 if( ChkIndex != MyWasp( Index )) {
  213.                     LONG NewDX, NewDY;
  214.  
  215.                     NewDX = WaspX( 1, ChkIndex ) - BeeX( 1, Index );
  216.                     NewDY = WaspY( 1, ChkIndex ) - BeeY( 1, Index );
  217.                     NewDistance = FastSQRT( NewDX * NewDX + NewDY * NewDY );
  218.                     if( Distance > NewDistance ) {
  219.                         DX = NewDX;
  220.                         DY = NewDY;
  221.                         if(!( NewDistance )) Distance = 1L;
  222.                         else Distance = NewDistance;
  223.                         SP->ss_NB[MyWasp( Index )]--;
  224.                         SP->ss_NB[MyWasp( Index ) = ChkIndex]++;
  225.                     }
  226.                 }
  227.             }
  228.         }
  229.  
  230.         BXVel( Index ) +=( DX * SP->ss_BeeAcc ) / Distance + RAND( 3 );
  231.         BYVel( Index ) +=( DY * SP->ss_BeeAcc ) / Distance + RAND( 3 );
  232.  
  233.         if( BXVel( Index ) > BEEVEL ) BXVel( Index ) = BEEVEL;
  234.         if( BXVel( Index ) < -BEEVEL ) BXVel( Index ) = -BEEVEL;
  235.         if( BYVel( Index ) > BEEVEL ) BYVel( Index ) = BEEVEL;
  236.         if( BYVel( Index ) < -BEEVEL ) BYVel( Index ) = -BEEVEL;
  237.  
  238.         BeeX( 0, Index ) = BeeX( 1, Index ) + BXVel( Index );
  239.         BeeY( 0, Index ) = BeeY( 1, Index ) + BYVel( Index );
  240.  
  241.         if(( BeeX( 0, Index ) < BORDER )||( BeeX( 0, Index ) > SP->ss_Width-BORDER-1 )) {
  242.             BXVel( Index ) = -BXVel( Index );
  243.             BeeX( 0, Index ) = BeeX( 1, Index ) + BXVel( Index );
  244.         }
  245.  
  246.         if(( BeeY( 0, Index ) < BORDER )||( BeeY( 0, Index ) > SP->ss_Height-BORDER-1 )) {
  247.             BYVel( Index ) = -BYVel( Index );
  248.             BeeY( 0, Index ) = BeeY( 1, Index ) + BYVel( Index );
  249.         }
  250.     }
  251.  
  252.     for( Index = 0L; Index < SP->ss_NumWasps; Index++ ) {
  253.         SetAPen( RP, 0 );
  254.         Move( RP, WaspX( 2, Index ), WaspY( 2, Index ));
  255.         Draw( RP, WaspX( 1, Index ), WaspY( 1, Index ));
  256.         SetAPen( RP, WASP_PEN );
  257.         Draw( RP, WaspX( 0, Index ), WaspY( 0, Index ));
  258.     }
  259.  
  260.     for( Index = 0L; Index < SP->ss_NumBees; Index++ ) {
  261.         SetAPen( RP, 0 );
  262.         Move( RP, BeeX( 2, Index ), BeeY( 2, Index ));
  263.         Draw( RP, BeeX( 1, Index ), BeeY( 1, Index ));
  264.         SetAPen( RP, BEE_PEN );
  265.         Draw( RP, BeeX( 0, Index ), BeeY( 0, Index ));
  266.     }
  267. }
  268.  
  269. VOID blank( VOID )
  270. {
  271.     struct    Screen        *SwarmScreen;
  272.     struct    SwarmStruct    *Swarms;
  273.     struct    mPrefObject    *mP;
  274.     LONG            Count;
  275.     WORD            Color, DColor;
  276.  
  277.     if( ASwarmWnd ) mP = &nP;
  278.     else mP = ( struct mPrefObject * )prefData;
  279.  
  280.     SwarmScreen = OpenScreenTags( NULL, SA_Depth, 2, SA_Overscan, OSCAN_STANDARD, SA_DisplayID, Mode, SA_Quiet,
  281.         TRUE, SA_Behind, TRUE, TAG_DONE );
  282.  
  283.     if( SwarmScreen ) {
  284.         SetRGB4( &( SwarmScreen->ViewPort ), 0, 0, 0, 0 );
  285.         SetRast( &( SwarmScreen->RastPort ), 0 );
  286.         BlankMousePointer();
  287.         ScreenToFront( SwarmScreen );
  288.  
  289.         if( Swarms = CreateSwarms( SwarmScreen, mP->Wasps, mP->Bees, mP->Speed + 1, mP->Tightness )) {
  290.             Color = BEE_COL_NUM-1;
  291.             DColor = mP->Colorcycling ? 1 : 0;
  292.             Count = mP->Speed + 1;
  293.  
  294.             while(!( SetSignal( 0, 0 )& SIGBREAKF_CTRL_C )){
  295.                 SwarmColors[BEE_PEN] = BeeColors[Color];
  296.                 LoadRGB4( &( SwarmScreen->ViewPort ), SwarmColors, 4 );
  297.                 Color += DColor;
  298.                 if(( Color == -1 )||( Color == BEE_COL_NUM )) {
  299.                     DColor = -DColor;
  300.                     Color += 2 * DColor;
  301.                 }
  302.  
  303.                 WaitTOF();
  304.  
  305.                 if( ++Count > MAX_SPEED ) {
  306.                     Count = mP->Speed + 1;
  307.                     DrawSwarms( &( SwarmScreen->RastPort ), Swarms, mP->Aimmode );
  308.                 }
  309.             }
  310.             SetSignal( 0L, SIGBREAKF_CTRL_C );
  311.             FreeVec( (APTR)Swarms );
  312.         }
  313.         UnblankMousePointer();
  314.         CloseScreen( SwarmScreen );
  315.     }
  316. }
  317.